<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class core_mail
{
	private $mail;
	private $mail_transport;
	private $charset = 'utf-8';
	private $queue = true;
	private $queue_per_time = 10;
	public $debug = false;
	
	public function connect($queue = true, $email_type = '', $smtp_config = array())
	{
		$this->queue = $queue;
		
		if (!$email_type)
		{
			$email_type = get_setting('email_type');
		}
		
		switch ($email_type)
		{
			case 1:	// SMTP
				if(empty($smtp_config))
				{
					$auth = array(
						'auth' => 'login',
						'username' => get_setting('smtp_username'),
						'password' => get_setting('smtp_password')
					);
					
					if(intval(get_setting('smtp_port')) > 0)
					{
						$auth['port'] = intval(get_setting('smtp_port'));
					}
					
					if(get_setting('smtp_ssl') == 'Y')
					{
						$auth['ssl'] = 'ssl';
					}
					
					$smtp_server = get_setting('smtp_server');
				}
				else
				{
					$auth = array(
						'auth' => 'login',
						'username' => $smtp_config['smtp_username'],
						'password' => $smtp_config['smtp_password'],
					);
					
					if(intval($smtp_config['smtp_port']) > 0)
					{
						$auth['port'] = intval($smtp_config['smtp_port']);
					}
					
					if($smtp_config['smtp_ssl'] == 'Y')
					{
						$auth['ssl'] = 'ssl';
					}
					
					$smtp_server = $smtp_config['smtp_server'];
				}
				
				try 
				{
					$this->mail_transport = new Zend_Mail_Transport_Smtp($smtp_server, $auth);
				}
				catch (Exception $e)
				{
					if($this->debug)
					{
						echo $e->getMessage();
						die;
					}
					
					return false;
				}
			break;
			
			case 2:	// Sendmail
				try 
				{
					$this->mail_transport = new Zend_Mail_Transport_Sendmail(get_setting('from_email'));
				}
				catch (Exception $e)
				{
					if($this->debug)
					{
						echo $e->getMessage();
						die;
					}
					
					return false;
				}
			break;
		}
				
		return $this->mail_transport;
	}

	/**
	 * 
	 * @param  $from_email
	 * @param  $from_name
	 * @param  $to_email
	 * @param  $to_name
	 * @param  $title
	 * @param  $body
	 */
	public function send_mail($from_email, $from_name, $to_email, $to_name, $title, $body, $level = 0, $debug = false)
	{
		if (empty($from_email))
		{
			$from_email = get_setting('from_email');
		}
		
		if ($this->queue == false)
		{
			if (!$this->mail_transport)
			{
				$this->connect();
			}
			
			try
			{
				$mail = new Zend_Mail($this->charset);
				$mail->setBodyHtml($body);
				$mail->setFrom($from_email, $from_name);
				$mail->addTo($to_email, $to_name);
				$mail->setSubject($title);
				$mail->send($this->mail_transport);
				return true;
			}
			catch (Exception $e)
			{
				if ($this->debug)
				{
					echo $e->getMessage();
					die;
				}
				
				return false;
			}
		}
		else
		{
			$insert_arr["from_email"] = FORMAT::mysql_safe($from_email);
			$insert_arr["from_name"] = FORMAT::mysql_safe($from_name);
			$insert_arr["to_email"] = FORMAT::mysql_safe($to_email);
			$insert_arr["to_name"] = FORMAT::mysql_safe($to_name);
			$insert_arr["title"] = $title;
			$insert_arr["body"] = $body;
			$insert_arr["add_time"] = mktime();
			$insert_arr["level"] = $level;
			
			AWS_APP::db()->insert(get_table("mail_queue"), $insert_arr);
			
			return AWS_APP::db()->lastInsertId();
		}
	}
}